﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AjaxControlFramework
{
    [AttributeUsage(AttributeTargets.Class)]
    public class AjaxControlOptionsAttribute : Attribute
    {
        //------// Properties \\--------------------------------------------\\
        private bool? _loadOutOfBand = null;
        public bool? LoadOutOfBand
        {
            get { return _loadOutOfBand; }
            set { _loadOutOfBand = value; }
        }


        private ScriptBuildMode _buildMode = ScriptBuildMode.Release;
        public ScriptBuildMode BuildMode
        {
            get { return _buildMode; }
            set { _buildMode = value; }
        }


        public Type CallbackStrategyType { get; set; }
        public Type StateStrategyType { get; set; }
        public Type ScriptStrategyType { get; set; }
        public Type AuthenticationStrategyType { get; set; }
        public Type CallbackSerializerType { get; set; }


        private string[] _authorizedRoles = null;
        public virtual string[] AuthorizedRoles
        {
            get { return _authorizedRoles; }
            set
            {
                _authorizedRoles = value;

                if (_authorizedRoles != null)
                {
                    ValidateAuthorizedRoles(_authorizedRoles);
                }
            }
        }


        public virtual bool Protected
        {
            get { return (AuthorizedRoles != null && AuthorizedRoles.Length > 0); }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public AjaxControlOptionsAttribute()
        { }


        public AjaxControlOptionsAttribute(params string[] authorizedRoles)
            : this()
        {
            AuthorizedRoles = authorizedRoles;
        }


        public AjaxControlOptionsAttribute(bool loadOutOfBand, ScriptBuildMode buildMode)
        {
            LoadOutOfBand = loadOutOfBand;
            BuildMode = buildMode;
        }


        public AjaxControlOptionsAttribute(bool loadOutOfBand, ScriptBuildMode buildMode, params string[] authorizedRoles)
            : this(loadOutOfBand, buildMode)
        {
            AuthorizedRoles = authorizedRoles;
        }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        protected virtual void ValidateAuthorizedRoles(string[] authorizedRoles)
        {
            if (authorizedRoles == null) { return; }

            foreach (string authorizedRole in authorizedRoles)
            {
                if (String.IsNullOrWhiteSpace(authorizedRole))
                {
                    throw new InvalidUserRolesException("The array of authorized roles contain one or more null/empty string values.");
                }
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
